package com.lxmes.device.utils;

import com.lxmes.common.config.RuoYiConfig;
import com.lxmes.common.utils.StringUtils;
import net.coobird.thumbnailator.Thumbnails;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigDecimal;

public class WaterMarkUtils {
    /**
     * @param imgPath  图片地址
     * @param  text 水印文字
     * @param  date 日期
     */
    public static void creat(String imgPath,String text,String date) {
        // 原图位置, 输出图片位置, 水印文字颜色, 水印文字
        String destPath = RuoYiConfig.getUploadPath();
        if(StringUtils.isNotEmpty(imgPath)){
            int dirLastIndex = "/profile/upload".length();
            String currentDir = StringUtils.substring(imgPath, dirLastIndex);
            imgPath=destPath+currentDir;
        try {
            // 读取原图片信息
            File srcImgFile = new File(imgPath);
            Image srcImg = ImageIO.read(srcImgFile);
            int srcImgWidth = srcImg.getWidth(null);
            int srcImgHeight = srcImg.getHeight(null);
            // 加水印
            BufferedImage bufImg = new BufferedImage(srcImgWidth,
                    srcImgHeight,
                    BufferedImage.TYPE_INT_RGB);
            //获取 Graphics2D 对象
            Graphics2D g = bufImg.createGraphics();
            //设置绘图区域
            g.drawImage(srcImg, 0, 0, srcImgWidth, srcImgHeight, null);
            //设置字体
            Font font = new Font("微软雅黑", Font.PLAIN, 23);
            // 根据图片的背景设置水印颜色
            g.setColor(Color.GREEN);
            g.setFont(font);
            //获取文字长度
            int x =0;
            int y=0;
            int len =0;
             len = g.getFontMetrics(
                    g.getFont()).charsWidth(text.toCharArray(),
                    0,
                     text.length());
             x = srcImgWidth - len - 10;
             y = srcImgHeight - 60;
            g.drawString(text, x, y);
            g.setFont(font);
            g.setColor(Color.GREEN);
            len = g.getFontMetrics(
                    g.getFont()).charsWidth(date.toCharArray(),
                    0,
                    date.length());
            x = srcImgWidth - len - 2;
            y = srcImgHeight - 30;
            g.drawString(date, x, y);

            g.dispose();
            // 输出图片
            FileOutputStream outImgStream = new FileOutputStream(imgPath);
            ImageIO.write(bufImg, "png", outImgStream);
            outImgStream.flush();
            outImgStream.close();
            System.out.println("OK");
        } catch (Exception e) {
            e.printStackTrace();
         }
        }
    }
    /***
     * 功能 :按照正方形缩放图片，精度较高
     * 处理100X100或者别的
     * @param imgPath 原图片路径
     * @param imgPath  转换大小后图片路径
     * @param width   转换后图片宽度
     * @param height  转换后图片高度
     */
    public static void resizeImage(String imgPath,int width, int height) throws IOException, IOException {
        String subfix = "jpg";
        subfix = imgPath.substring(imgPath.lastIndexOf(".")+1,imgPath.length());
        String destPath = RuoYiConfig.getUploadPath();
        int dirLastIndex = "/profile/upload".length();
        String currentDir = StringUtils.substring(imgPath, dirLastIndex);
        imgPath=destPath+currentDir;
        File srcFile = new File(imgPath);
        Image srcImg = ImageIO.read(srcFile);
        BufferedImage buffImg = null;
        if(subfix.equals("png") || subfix.equals("PNG")){
            buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        }else{
            buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        }
        Graphics2D graphics = buffImg.createGraphics();
        graphics.setBackground(Color.WHITE);
        graphics.setColor(Color.WHITE);
        graphics.fillRect(0, 0, width, height);
        graphics.drawImage(srcImg.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null);
        ImageIO.write(buffImg, subfix, new File(imgPath));
    }
    public static void commPressPicCycle(String desPath , double accuracy) throws IOException {
        String destPath = RuoYiConfig.getUploadPath();
        int dirLastIndex = "/profile/upload".length();
        String currentDir = StringUtils.substring(desPath, dirLastIndex);
        desPath=destPath+currentDir;
        File imgFile = new File(desPath);
        long fileSize = imgFile.length();
        //判断大小,如果小于500k,不压缩,如果大于等于500k,压缩
        //计算宽高
        BufferedImage bim = ImageIO.read(imgFile);
        int imgWidth = bim.getWidth();
        int imgHeight = bim.getHeight();
        int desWidth = new BigDecimal(imgWidth).multiply(
                new BigDecimal(accuracy)).intValue();
        int desHeight = new BigDecimal(imgHeight).multiply(
                new BigDecimal(accuracy)).intValue();
        Thumbnails.of(desPath).size(desWidth, desHeight).outputQuality(accuracy).toFile(desPath);
    }
}
